/*================================================================
*   Copyright (C) 2020 Sangfor Ltd. All rights reserved.
*   
*   文件名称：wait.c
*   创 建 者：Ann
*   创建日期：2020年04月25日
*   描    述：
*
================================================================*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main()
{
    int pid = fork();
    if(pid < 0)
    {
        perror("fork");
        return 0;
    }
    else if(pid == 0)
    {
        //child
        printf("i am child\n");
        sleep(5);
        exit(15);
    }
    else
    {
        //father
        int status;
        //int wait(int* status);
        wait(&status);
        if((status & 0x7F) == 0)
        {
            printf("正常退出\n");
            printf("exitcode=%d\n", (status >> 8) & 0xFF);
        }
        else
        {
            printf("异常退出\n");
            printf("codeflag=%d\n", (status >> 7) & 0x1);
        }
        while(1)
        {
            sleep(3);
            printf("i am father, i have no time\n");
        }
    }
    return 0;
}
